﻿using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web.Configuration;

public class FSO
{
    public static void Copy(string oldFilePath, string newFilePath)
    {
        string extension = GetExtension(oldFilePath);
        string str2 = GetExtension(newFilePath);
        switch (extension)
        {
            case "":
            case null:
                if (PathValid(oldFilePath))
                {
                    PathExists(newFilePath);
                    switch (str2)
                    {
                        case "":
                        case null:
                        {
                            DirectoryInfo info = new DirectoryInfo(oldFilePath);
                            CopyDirInfo(info, oldFilePath, newFilePath);
                            return;
                        }
                    }
                    DirectoryInfo od = new DirectoryInfo(oldFilePath);
                    FileInfo info3 = new FileInfo(newFilePath);
                    CopyDirInfo(od, oldFilePath, info3.DirectoryName);
                    return;
                }
                break;

            default:
                if (PathValid(oldFilePath))
                {
                    PathExists(newFilePath);
                    switch (str2)
                    {
                        case "":
                        case null:
                            File.Copy(oldFilePath, newFilePath + @"\" + Path.GetFileName(oldFilePath), true);
                            return;
                    }
                    File.Copy(oldFilePath, newFilePath, true);
                }
                break;
        }
    }

    public static void CopyDirInfo(DirectoryInfo od, string oldDir, string newDir)
    {
        PathExists(newDir);
        foreach (DirectoryInfo info in od.GetDirectories())
        {
            CopyDirInfo(info, info.FullName, newDir + info.FullName.Replace(oldDir, ""));
        }
        foreach (FileInfo info2 in od.GetFiles())
        {
            Copy(info2.FullName, newDir + info2.FullName.Replace(oldDir, ""));
        }
    }

    public static void Del(string filePath)
    {
        switch (GetExtension(filePath))
        {
            case "":
            case null:
                if (PathValid(filePath))
                {
                    Directory.Delete(filePath, true);
                    return;
                }
                break;

            default:
                if (FileValid(filePath))
                {
                    File.Delete(filePath);
                }
                break;
        }
    }

    public static bool ExtensionValid(string extension, string extensionList)
    {
        string[] strArray = extensionList.Split(new char[] { '|' });
        for (int i = 0; i < strArray.Length; i++)
        {
            if (extension == strArray[i])
            {
                return true;
            }
        }
        return false;
    }

    public static string FileSize(string filePath)
    {
        if (FileValid(filePath))
        {
            FileInfo info = new FileInfo(filePath);
            return info.Length.ToString();
        }
        return "0";
    }

    public static bool FileValid(string filePath)
    {
        return File.Exists(filePath);
    }

    public static SortedList FolderLsit(string filePath)
    {
        SortedList list = new SortedList();
        DirectoryInfo[] directories = new DirectoryInfo(filePath).GetDirectories();
        int key = 0;
        foreach (DirectoryInfo info2 in directories)
        {
            list.Add(key, info2.Name);
            key++;
        }
        return list;
    }

    public static string GetExtension(string str)
    {
        return Path.GetExtension(str).ToLower();
    }

    public static string GetUploadPath()
    {
        return WebConfigurationManager.AppSettings["UploadFilePath"];
    }

    public static void PathExists(string filePath)
    {
        FileInfo info = new FileInfo(filePath);
        if (!Directory.Exists(info.DirectoryName))
        {
            Directory.CreateDirectory(info.DirectoryName);
        }
    }

    public static bool PathValid(string filePath)
    {
        FileInfo info = new FileInfo(filePath);
        return Directory.Exists(info.DirectoryName);
    }

    public static string RandomFileName(string fileName)
    {
        string extension = GetExtension(fileName);
        return fileName.Replace(extension, "_" + Function.RandNum(6) + extension);
    }

    public static string ReadFile(string filePath)
    {
        StreamReader reader = new StreamReader(filePath, Encoding.UTF8);
        using (reader)
        {
            return reader.ReadToEnd();
        }
    }

    public static bool WriteFile(string filePath, string fileContent)
    {
        bool flag;
        PathExists(filePath);
        StreamWriter writer = new StreamWriter(filePath, false, Encoding.UTF8);
        StreamWriter writer2 = writer;
        try
        {
            writer.Write(fileContent);
            flag = true;
        }
        catch
        {
            flag = false;
        }
        finally
        {
            if (writer2 != null)
            {
                writer2.Dispose();
            }
        }
        return flag;
    }
}

